/** 
 * Copyright (C), 2010-2012, TIETIAO Software Co.,Ltd. ALL RIGHTS RESERVED. 
 * File name:DateFormater.java
 * Version:1.0
 * Date:2011-6-16
 * Description: Date格式化工具类
 * author:zhijian.zhang
 */
package com.tietiao.common.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.tietiao.common.constants.CommodityConstants;

public class DateFormater {

	public static final String[] DATA_FORMATS = new String[] { "yyyy-MM-dd",
			"yyyy-MM-dd HH:mm:ss","yyyy/MM/dd" };

	public static SimpleDateFormat DATE_AND_TIME_FORMATER = new SimpleDateFormat(
			DATA_FORMATS[1]);

	public static SimpleDateFormat YEAR_MONTH_DAY_FORMATER = new SimpleDateFormat(
			DATA_FORMATS[0]);
	
	public static SimpleDateFormat YEAR_MONTH_DAY_FILE_FORMATER = new SimpleDateFormat(DATA_FORMATS[2]);

	/**
	 * 
	 * 功能：将指定的日期格转换为"yyyy-MM-dd HH:mm:ss"字符串
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-6-16
	 * 
	 * @param date
	 * @return "yyyy-MM-dd HH:mm:ss"字符串
	 */
	public static String formatDateAndTime(Date date) {
		return formatDate(date, DATE_AND_TIME_FORMATER);
	}

	/**
	 * 
	 * 功能：将指定格式"yyyy-MM-dd HH:mm:ss"的日期字符串转换成Timestamp类型的数据
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-6-16
	 * 
	 * @param date
	 * @return Timestamp
	 */
	public static Timestamp dateAntTimeStringToTimestamp(String date) {
		return dateStringToTimestamp(date, DATE_AND_TIME_FORMATER);
	}

	/**
	 * 
	 * 功能：将指定格式"yyyy-MM-dd HH:mm:ss"的日期字符串转换成Date类型的数据
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-6-16
	 * 
	 * @param date
	 * @return Date
	 */
	public static Date dateAntTimeStringToDate(String date) {
		return dateStringToDate(date, DATE_AND_TIME_FORMATER);
	}

	public static Date addDayOnDate(Date date ,int add){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_YEAR, add);
		return cal.getTime();
	}
	/**
	 * 
	 * 功能：将指定的日期格转换为字符串
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-6-16
	 * 
	 * @param date
	 * @return 日期字符串
	 */
	private static String formatDate(Date date, SimpleDateFormat dateFormat) {
		return dateFormat.format(date);
	}

	/**
	 * 
	 * 功能：将指定的日期字符串转换成Timestamp类型的数据
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-6-16
	 * 
	 * @param date
	 * @return Timestamp
	 */
	private static Timestamp dateStringToTimestamp(String date,
			SimpleDateFormat dateFormat) {
		try {
			return new Timestamp(dateFormat.parse(date).getTime());
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 
	 * 功能：将指定的日期字符串转换成Date类型的数据
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-6-16
	 * 
	 * @param date
	 * @return Date
	 */
	public static Date dateStringToDate(String date, SimpleDateFormat dateFormat) {
		try {
			return dateFormat.parse(date);
		} catch (ParseException e) {
			return null;
		}
	}
	public static void main(String[] args) {
		System.out.println( CommodityConstants.ROOT_FILE_PATH +"\\" + DateFormater.YEAR_MONTH_DAY_FILE_FORMATER
				.format(new Date()));
	}
}
